package com.dja.nzw.mq.rabbit.producer;

import com.dja.nzw.mq.rabbit.EndPoint;
import com.dja.nzw.mq.rabbit.config.AMQPConfig;
import com.dja.nzw.mq.rabbit.dto.MessagePayload;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;

/**
 * Created by shuyun on 2016/8/8.
 */
public class Producer extends EndPoint {
    private RabbitTemplate rabbitTemplate;

    public Producer(AMQPConfig amqpConfig) {
        super(amqpConfig);
        rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setExchange(MessagePayload.exchangeName());
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
    }

    public void publish(String routingKey, MessagePayload messagePayload){
        rabbitTemplate.convertAndSend(routingKey, messagePayload);
    }
}
